package com.iespring.controller.app.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.iespring.bean.app.user.bo.UserRegisterBo;
import com.iespring.bean.app.user.bo.WxResetBo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.service.app.userregister.WxUserRegisterService;
import com.iespring.util.SMSUtils;
import com.iespring.util.ValidationUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * Auth: 杨宇健
 * Time: 2023/1/2 14:31
 * Description:
 **/
@RestController
@RequestMapping("wx")
public class WxRegisterController {
    @Resource
    private WxUserRegisterService wxUserRegisterService;

    @PostMapping("auth/regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody Map<String,String> mobile, HttpServletRequest request){
        String uri = request.getRequestURI();
        //调用不同的工具类，保证发的验证码存储位置不一致
        try {
            //无法区分，只能混用
            SMSUtils.SMSSend(mobile.get("mobile"));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return BaseRespVo.error("短信发送失败", 500);
        }
        return BaseRespVo.ok();
    }

    @PostMapping("auth/register")
    public BaseRespVo register(@RequestBody @Valid UserRegisterBo user, BindingResult bindingResult) throws Exception {
        ValidationUtils.validParam(bindingResult);
        BaseRespVo respVo = wxUserRegisterService.register(user);
        return respVo;
    }

    @PostMapping("auth/reset")
    public BaseRespVo reset(@RequestBody @Valid WxResetBo reset, BindingResult bindingResult) throws Exception {
        ValidationUtils.validParam(bindingResult);
        BaseRespVo respVo = wxUserRegisterService.reset(reset);
        return respVo;
    }
}
